<?php
$path = dirname(dirname(__FILE__));
require("$path/lib/crt.php");
require("$path/lib/crtuser.php");

$crt = new CRT();
$crt->init();

// Only editors allowed here
$user = $crt->getUser();
if($user->role != EDITOR_ROLE) {
        header("Location: /index.php");
        exit;
}


// Perform any requested operations
$op = Clean::string($_REQUEST['op'], false, false, true);

switch($op) {
	
	// Add a user
	case 'add' :
		$username = Clean::string($_REQUEST['username'], false, false, true);
		$password = Clean::string($_REQUEST['password'], false, false, true);
		$role = Clean::posInt($_REQUEST['role'], false, true);

		if($username && $password && $role) {
			// Check for existing username
			$existingUser = CRTUser::getUserByLogin($username);
			if($existingUser) {	
				$msg = "The username '$username' already exists in the database";
			} else {
				CRTUser::add($username, $password, $role);
				$msg = "User successfully added";
			} 
		} else {
			$msg = "Information missing. Unable to add user.";
		}

		break;

	// Delete a user
	case 'delete' :
		$userId = Clean::posInt($_REQUEST['u']);
		$user = new CRTUser($userId);
		$user->delete();

		break;

	// Update an individual user's role
	case 'role' :
		$userId = Clean::int($_REQUEST['userId'],false, true);
		$role = Clean::int($_REQUEST['roleChange'], false, true);

		if($userId && $role) {
			$user = new CRTUser($userId);
			$user->updateRole($role);
		}

		break;

	default: 
		break;
}

$users = CRTUser::getUsers();

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>HQ CRT</title>
        <?php
        $dir = opendir(PLUGIN_PATH.'/css');
        while(($file= readdir($dir))!==false) {
            echo '<link rel="stylesheet" href="/css/'.$file.'" type="text/css" media="all">';
        }

        ?>

	<script type='text/javascript' src='/js/jquery/jquery-1.3.2.min.js'></script>

    </head>

    <body>

        <div id='acctWrap'>
		<h1>Account Management</h1>
		<span><a href='/index.php'>Back to map</a></span>
		<div style="padding: 5px; color: red; font-weight: bold;"><?php echo $msg; ?></div>

		<form method="POST">

		<table id='userAccounts'>
			<tr><td><strong>Username</strong></td><td><strong>Role</strong></td><td>&nbsp;</td></tr>

		<?php
		foreach($users as $u) {
			echo "<tr><td>{$u['username']}</td>";
			echo "<td>"; 
			
			$iSelected = $eSelected = '';

			if($u['role'] == INFLUENCER_ROLE) $iSelected = "selected";
			elseif($u['role'] == EDITOR_ROLE) $eSelected = "selected";

			$select = "<select name='roleSelect' userId='{$u['id']}' >
                                <option $iSelected value='".INFLUENCER_ROLE."'>Influencer</option>
                                <option $eSelected value='".EDITOR_ROLE."'>Editor</option>
                        </select>";
			
			echo $select;
			echo "</td>";

			echo "<td><a a href='/user/accounts.php?op=delete&u={$u['id']}'>Delete</a></td></tr>";
		}
		?>

		</table>
		<br /><br />
		<div>
			<strong>Add User</strong>
			<table id='addUserTbl'>
				<tr><td>Username:</td><td><input type='text' name='username' value='' /></td></tr>
				<tr><td>Password:</td><td><input type='text' name='password' value='' /></td></tr>
				<tr><td>Role:</td>
					<td>
						<select name='role'>
							<option value='<?php echo INFLUENCER_ROLE; ?>'>Influencer</option>
							<option value='<?php echo EDITOR_ROLE; ?>'>Editor</option>
						</select>
					</td></tr>
				<tr><td colspan='2'>&nbsp;</td></tr>
				<tr><td colspan='2'>
					<input type='hidden' name='userId' value='' />
					<input type='hidden' name='roleChange' value='' />
					<input type='hidden' name='op' value='add' /><input type='submit' name='submit' value='Submit' />
				</td></tr>
			</table>
		</div>
		
                </form>
        </div>


<script type='text/javascript'>
var $j = jQuery.noConflict();
$j('select').change(function(evt) {
	var $tgt = $j(evt.target);
	var userId = $tgt.attr('userId');
	var opt = $tgt.find('option:selected');

	$j("input[name='userId']").val(userId);
	$j("input[name='roleChange']").val(opt.val());
	$j("input[name='op']").val('role');
	$j("input[name='submit']").trigger('click');
		
});
</script>

</body>
</html>

